package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.BusinessException;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Employee;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl
        extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public void remove(Long id) {
        //1、检查当前分类下是否有对应的菜品
        // SELECT COUNT(*) FROM `dish`
        // WHERE category_id = 1397844263642378242
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,id);
        Integer count = dishMapper.selectCount(qw);
        if(count>0){
            //不能删除 TODO 程序结束 自定义一个业务异常，然后抛出
            throw new BusinessException("该分类下有菜品，不能删除");
        }

        //2、检查当前分类下是否有对应的套餐
        // SELECT COUNT(*) FROM `setmeal`
        // WHERE category_id= 1397844263642378242
        LambdaQueryWrapper<Setmeal> qw2 = new LambdaQueryWrapper<>();
        qw2.eq(Setmeal::getCategoryId,id);
        Integer count2 = setmealMapper.selectCount(qw2);
        if(count2>0){
            //不能删除  TODO 程序结束 自定义一个业务异常，然后抛出
            throw new BusinessException("该分类下有套餐，不能删除");
        }

        //3、可以删除
        categoryMapper.deleteById(id);
    }
}
